home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-02 / binfile.zip / BINFILE.INT < prev    next >
Text File  |  1993-03-14  |  14KB  |  272 lines

  1. Unit BinFile ;
  2. {$O+ F+}
  3.  
  4. Interface
  5.  
  6. {         Program Unit name  : BinFile.TPU
  7.           Unit source code   : BinFile.PAS
  8.           Interface File     : BinFile.INT
  9.           Documentation File : BinFile.DOC
  10.           Version 1.0
  11.           Written by Donald St. Patrick Golding Jr.
  12.           Date of Creation : March 10, 1993
  13.           Purpose Of Unit  : This unit contains the BinaryFileObj Object.
  14.                              It allows flexible of a binary file.
  15.  
  16.   Note : File offsets start from 0 and range to an offset equal to the size
  17.          of the file.
  18. }
  19.  
  20. Uses Crt, Dos ;
  21.  
  22. Const
  23.  
  24.    { Error Codes }
  25.    NoError             =  0  ;
  26.    Err_FileNotFound    = -1  ;
  27.    Err_NoDiskSpace     = -2  ;
  28.    Err_InvalidOffSet   = -3  ;
  29.    Err_InvalidDataSize = -4  ;
  30.    Err_DriveNotReady   = -5  ;
  31.    Err_ReadError       = -6  ;
  32.    Err_NotEnoughSpace  = -7  ;
  33.    Err_FileNotOpen     = -8  ;
  34.    Err_NotEnoughMem    = -9  ;
  35.    Err_DiskIO          = -10 ;
  36.    Err_InvalidFileSize = -11 ;
  37.    Err_Unknown         = -50 ;
  38.  
  39.    { Processing Constants }
  40.    NextWrite           = -1  ;
  41.    NextRead            = -2  ;
  42.    DefaultStartOffSet  = -3  ;
  43.    DefaultEndOffSet    = -4  ;
  44.    DefaultOffSetFactor = -5  ;
  45.  
  46. Type
  47.  
  48.    OrderType = (Ascending, Descending) ;
  49.    CompareType = (CompEqual, CompGreater, CompLess) ;
  50.    CompareFuncType = Function (OffSet1, OffSet2 : LongInt ;
  51.                                Var Data1, Data2) : CompareType ;
  52.    SearchFuncTypeL = Function (FileOffSet : LongInt;
  53.                                Var DataToFind, DataRead) : Boolean ;
  54.    SearchFuncTypeB = Function (FileOffSet : LongInt;
  55.                                Var DataToFind, DataRead) : CompareType ;
  56.  
  57.    BinaryFileObjPtr = ^BinaryFileObj ;
  58.    PerformType      = Procedure (FileOffSet : LongInt; Var Data; DSize : Word;
  59.                                  BinObjPtr : BinaryFileObjPtr) ;
  60.    BinaryFileObj = Object
  61.  
  62.                 Constructor Init (FileName : String; NewFile : Boolean;
  63.                                   DataS : Word) ;
  64.                 { Initializes the file object and must be called first.
  65.                   FILENAME is the name of the file to be accessed by the
  66.                   object.  NEWFILE indicated whether or not the file should
  67.                   be considered a new file.  If the file already exists then
  68.                   the existing file's data is erased.  Otherwise the file is
  69.                   created.  DATAS is the size of data to be stored in the
  70.                   file. This number is used as the default data size if it
  71.                   is greater than 0.  If DATAS is 0 then the file's data will
  72.                   be retrieved dynamically.  Any access to data in the file
  73.                   (reading or writing) this data size will have to be
  74.                   indicated.
  75.                 }
  76.  
  77.                 Destructor Done (FileErase : Boolean) ;
  78.                 { This object uninitialized all allocated memory and
  79.                   releases the file.  If FILEERASE is true then the file is
  80.                   erased.
  81.                 }
  82.  
  83.                 Procedure ReadData (FileOffSet : LongInt; DataS : Word;
  84.                                     Var Data; Var SuccessFul : Boolean) ;
  85.                 { This method allows you to read the data from the file.
  86.                   FILEOFFSET is the offset into the file to start the read.
  87.                   If FILEOFFSET is less than 0 then reading will start from
  88.                   the current file offset.  DATAS is the size of the data to
  89.                   be read.  If the data size that was indicated in the
  90.                   BinaryFileObj.Init method was greater than 0 then this
  91.                   value is ignored.  If the size was not stated in the
  92.                   BinaryfileObj.Init method then this value is check.  If
  93.                   DATAS is 0 then an error will be indicated. DATA is the
  94.                   variable that will recieve the data that is read from the
  95.                   data file.  SUCCESSFUL is true is the read of the file
  96.                   was accomplished.
  97.                 }
  98.  
  99.                 Procedure WriteData (FileOffSet : LongInt; DataS : Word;
  100.                                      Var Data; Var SuccessFul : Boolean) ;
  101.                 { Same a ReadData except data is written. }
  102.  
  103.                 Procedure Copy (SrcOffSet, DestOffSet : LongInt;
  104.                                 DataS : Word ; Var SuccessFul : Boolean) ;
  105.                 { Copies data from one file offset to another.  SRCOFFSET is
  106.                 the file offset for the source data to be copied.
  107.                 DESTOFFSET is the destination file offset.  DATAS is the
  108.                 size of the data.  If the data size that was indicated in
  109.                 the BinaryFileObj.Init method was greater than 0 then this
  110.                 value is ignored.  If the size was not stated in the
  111.                 BinaryfileObj.Init method then this value is check.  If
  112.                 DATAS is 0 then an error will be indicated.  SUCCESSFUL is
  113.                 true if the copy was done correctly.
  114.                 }
  115.  
  116.                 Procedure Swap (OffSet1, OffSet2 : LongInt;
  117.                                 DataS : Word ; Var SuccessFul : Boolean) ;
  118.                 { Swaps data from one file offset to another.  OFFSET1 is
  119.                   the first file offset.  OFFSET2 is the second offset.
  120.                   DATAS is the size of the data.  If the data size that was
  121.                   indicated in the BinaryFileObj.Init method was greater
  122.                   than 0 then this value is ignored.  If the size was not
  123.                   stated in the BinaryfileObj.Init method then this value is
  124.                   check.  If DATAS is 0 then an error will be indicated.
  125.                   SUCCESSFUL is true if the swap was done correctly.
  126.                 }
  127.  
  128.                 Procedure BubbleSort (Order : OrderType; DataS : Word;
  129.                                       CompareFunc : CompareFuncType ;
  130.                                       StartOffS, EndOffS : LongInt ;
  131.                                       Var SuccessFul : Boolean) ;
  132.                 { Uses the Bubble Sort to sort the file.  ORDER is the order
  133.                   in which to sort the data file (Ascending or Descending).
  134.                   DATAS is the size of the data.  If the data size that was
  135.                   indicated in the BinaryFileObj.Init method was greater
  136.                   than 0 then this value is ignored.  If the size was not
  137.                   stated in the BinaryfileObj.Init method then this value is
  138.                   check.  If DATAS is 0 then an error will be indicated.
  139.                   COMPAREFUNC is the user defined function that is used to
  140.                   get a result of a comparison between data.  STARTOFFS is
  141.                   the starting offset in the file to start sorting.  If
  142.                   STARTOFFS is negative then the sort starts from the
  143.                   beginning of the file.  ENDOFFS is the ending offset to
  144.                   end the sort.  If ENDOFFS is negative then the ending
  145.                   offset is the last record in the data file. SUCCESSFUL is
  146.                   true if the sort was done correctly.
  147.                 }
  148.  
  149.                 Procedure QuickSort (Order : OrderType; DataS : Word;
  150.                                      CompareFunc : CompareFuncType ;
  151.                                      StartOffS, EndOffS : LongInt ;
  152.                                      Var SuccessFul : Boolean) ;
  153.                 { Uses the Quick Sort to sort the file.  (Faster than
  154.                   Bubble sort) ORDER is the order in which to sort the data
  155.                   file (Ascending or Descending).  DATAS is the size of the
  156.                   data.  If the data size that was indicated in the
  157.                   BinaryFileObj.Init method was greater than 0 then this
  158.                   value is ignored.  If the size was not stated in the
  159.                   BinaryfileObj.Init method then this value is check.  If
  160.                   DATAS is 0 then an error will be indicated. COMPAREFUNC is
  161.                   the user defined function that is used to get a result of
  162.                   a comparison between data.  STARTOFFS is the starting
  163.                   offset in the file to start sorting.  If STARTOFFS is
  164.                   negative then the sort starts from the beginning of the
  165.                   file.  ENDOFFS is the ending offset to end the sort.  If
  166.                   ENDOFFS is negative then the ending offset is the last
  167.                   record in the data file. SUCCESSFUL is true if the sort
  168.                   was done correctly.
  169.                 }
  170.  
  171.                 Procedure LinearSearch (SearchFunc : SearchFuncTypeL;
  172.                                         DataS : Word; Var DataToFind;
  173.                                         StartOffSet : LongInt ;
  174.                                         Var OffSet : LongInt ;
  175.                                         Var SuccessFul : Boolean) ;
  176.                 { Searches the file for the data in DATATOFIND variable.
  177.                   SEARCHFUNC is the user defined function which returns
  178.                   true if the data was found.  DATAS is the size of the
  179.                   data.  If the data size that was indicated in the
  180.                   BinaryFileObj.Init method was greater than 0 then this
  181.                   value is ignored.  If the size was not stated in the
  182.                   BinaryfileObj.Init method then this value is check.  If
  183.                   DATAS is 0 then an error will be indicated.  STARTOFFSET is
  184.                   the offset to start the search.  If negative then the
  185.                   search starts at the beginning of the file.  OFFSET will
  186.                   contain the file offset where the data was found.  If the
  187.                   data is not found then OFFSET equals -1.  SUCCESSFUL is
  188.                   true if the data was found.
  189.                 }
  190.  
  191.                 Procedure BinarySearch (SearchFunc : SearchFuncTypeB;
  192.                                         Order      : OrderType      ;
  193.                                         DataS : Word; Var DataToFind;
  194.                                         OffSetFactor : LongInt      ;
  195.                                         Var OffSet : LongInt ;
  196.                                         Var SuccessFul : Boolean) ;
  197.                 { Searches the file for the data in DATATOFIND variable.
  198.                   SEARCHFUNC is the user defined function which returns
  199.                   true if the data was found.  The only way that the binary
  200.                   search will work successfully is if the data file has
  201.                   already been sorted. ORDER is the order in which the file
  202.                   was sorted.  DATAS is the size of the data.  If the data
  203.                   size that was indicated in the BinaryFileObj.Init method
  204.                   was greater than 0 then this value is ignored.  If the
  205.                   size was not stated in the BinaryfileObj.Init method then
  206.                   this value is check.  If DATAS is 0 then an error will be
  207.                   indicated.  OFFSETFACTOR is the offset to start the search.
  208.                   Any data below the OFFSETFACTOR will be ignored.  If
  209.                   negative then the search starts at the beginning of the
  210.                   file. OFFSET will contain the file offset where the data
  211.                   was found.  If the data is not found then OFFSET equals -1.
  212.                   SUCCESSFUL is true if the data was found.
  213.                 }
  214.  
  215.                 Procedure PerformForAll (StartOffSet : LongInt; DataS : Word;
  216.                                          UserProcedure : PerformType) ;
  217.                 { Calls the user defined procedure USERPROCEDURE and passed
  218.                   to that procedure the current data being accessed by the
  219.                   PERFORMFORALL method and the current offset.  Refer to the
  220.                   declaration of the PERFORMTYPE variable type.  STARTOFFS
  221.                   is the offset in the file to start reading at.  If
  222.                   STARTOFFS is negative then this method starts at the
  223.                   beginning of the file.  DATAS is the size of the data.
  224.                   If the data size that was indicated in the
  225.                   BinaryFileObj.Init method was greater than 0 then this
  226.                   value is ignored.  If the size was not stated in the
  227.                   BinaryfileObj.Init method then this value is check.
  228.                   If DATAS is 0 then an error will be indicated.
  229.                 }
  230.  
  231.                 Procedure ReduceFileSize (NewFileSize : LongInt;
  232.                                           Var SuccessFul : Boolean) ;
  233.                 { Reduces the size of a file to size NEWFILESIZE.  The value
  234.                 of NEWFILESIZE must be greater than or equal to 0.
  235.                 SUCCESSFUL is true if the method completed the operation
  236.                 without error.
  237.                 }
  238.  
  239.                 Procedure ResetFile ;
  240.                 { Resets the file offsets to the beginning of the file. }
  241.  
  242.                 Function GetEof : Boolean ;
  243.                 { Indicates whether we are at the end of a file. }
  244.  
  245.                 Function GetFileSize : LongInt ;
  246.                 { Returns current file size }
  247.  
  248.                 Function GetOffSet : LongInt ;
  249.                 { Returns current file offset }
  250.  
  251.                 Function GetFileName : String ;
  252.                 { Returns file name }
  253.  
  254.                 Function GetDataSize : Word ;
  255.                 { Returns default data size }
  256.  
  257.                 Function GetFileOpen : Boolean ;
  258.                 { Returns if file is open. }
  259.  
  260.                 Function GetError : Integer ;
  261.                 { Returns current error code. }
  262.  
  263.                 Procedure ClearError ;
  264.                 { Sets the error code to NOERROR }
  265.  
  266.              End ;
  267.  
  268. Function NoSort (OffSet1, OffSet2 : LongInt ;
  269.                  Var Data1, Data2) : CompareType ;
  270.  
  271. Implementation
  272.